// VALIDATION DU FORMULAIRE

function Load_all () {

setInterval("loadChat()",50);
setInterval("loadLogs()",50);
setInterval("scroll_def()",50);

}


function valide() {
	
	var ok = true;
	var pseudo = document.getElementById("login").value;
	var erreur = document.getElementById("erreur");
	
	if(pseudo=="")	
	{
	ok = false;
	erreur.innerHTML = "<?php echo ERR_VIDE ; ?>";
	}
	else if(pseudo.length < 3)
	{
	ok = false; 
	erreur.innerHTML = "Votre pseudo doit comporter au moins 3 caracteres";
	}
	
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'recherche_pseudo.php?pseudo=' + pseudo );
	
	xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4 && xhr.status == 200) {
	
	//alert(xhr.responseText);
	if(xhr.responseText == "2")
	 {
		
            erreur.innerHTML = '<span>' + pseudo + ' existe deja, veuillez trouver un autre pseudo' + '</span>'; 
	   ok = xhr.responseText;
	
	    
	    }
	    }
        };
	
	 xhr.send(null);
	 
	alert("coucou");
	 if(ok == "2" || ok == false)
	  ok = false;
	 else
	  ok = true ;
	  
	alert("coucou");
	return ok;
}


function dis(check) {
		
var offline = document.getElementById("Off");
var online = document.getElementById("On");

if(check=="On")
offline.checked= false;
else{
online.checked= false;
document.getElementsById("texte").disabled=true;
}
	}


// INSERER LES TAGS DE STYLE

function insertTag(startTag, endTag) {
		
    var field  = document.getElementById('champ_texte'); 
    var scroll = field.scrollTop;                    
    field.focus(); 
        
       var startSelection   = field.value.substring(0, field.selectionStart);
        var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
        var endSelection     = field.value.substring(field.selectionEnd);
	
field.value = startSelection + startTag + currentSelection + endTag + endSelection;
        field.focus();
        field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);

        field.scrollTop = scroll;
}

// CHARGER LE TEXTE DU CHAT

function loadChat() {
      var xhr = new XMLHttpRequest();

    xhr.open('GET', 'chat.txt');

    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4 && xhr.status == 200) { 
            document.getElementById('zone_texte').innerHTML = '<span>' + xhr.responseText + '</span>'; 
        }
    };
   xhr.send(null); 
}


// CHARGER LES UTILISATEURS CONNECTES

function loadLogs() {
      var xhr = new XMLHttpRequest();

    
    xhr.open('GET', 'logs/logins.txt');

    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4 && xhr.status == 200) { 
            document.getElementById('zone_logins').innerHTML = '<span>' + xhr.responseText + '</span>' ; 
        }
    };
   xhr.send(null); 
}


// FONCTIONS POUR GERER LA BARRE DE SCROLL //

var scrolled = 0;

function scroll_def() {


	var scroll = document.getElementById("zone_texte");
	
	if(scrolled==0)
	{
	scroll.scrollTop = scroll.scrollHeight;
	setTimeout("def()",10);
	}

}


	
function def()

{
   scrolled = 0 ;
}



function scroll() {
	
   var d = document.getElementById("zone_texte");



   var val = d.scrollTop + d.clientHeight - d.scrollHeight ;


   
if(scrolled == 0)
	scrolled =0;
else
	scrolled =1;
   scrolled = (val==0)?0:1 ;
	

}

// FIN FONCTIONS SCROLLLING //


// FONCTION DECONNEXION LORS DE LA FERMETURE DU NAVIGATEUR
function quit() {
 var xhr = new XMLHttpRequest();
  
    xhr.open('POST', 'deconnexion.php');
    xhr.send(null);
}

// FONCTION DE DETECTION DU TYPE DE NAVIGATEUR UTILISE
function detection_mobile ()
{
	if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
		return true;

	if (isset ($_SERVER['HTTP_ACCEPT']))
	{
		$accept = strtolower($_SERVER['HTTP_ACCEPT']);
		if (strpos($accept, 'wap') !== false)
			return true;
	}

	if (isset ($_SERVER['HTTP_USER_AGENT']))
	{
		if (strpos ($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false)
			return true;

		if (strpos ($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false)
			return true;
	}

	return false;
}






